package com.nowcoder.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.mapper.CommentMapper;
import com.nowcoder.community.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nowcoder.community.service.IDiscussPostService;
import com.nowcoder.community.util.SensitiveFilter;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朱哥
 * @since 2025-02-27
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private IDiscussPostService discussPostService;

    @Override
    public List<Comment> findCommentsByEntity(int entityType, Integer entityId, int offset, int limit) {
        return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit);
    }

    @Override
    public int findCommentCount(int entityType, Integer entityId) {
        long count = count(new QueryWrapper<Comment>().eq("entity_type", entityType).eq("entity_id", entityId).eq("status",0));
        try {
            return Math.toIntExact(count);
        } catch (ArithmeticException e) {
            throw new RuntimeException("Count is too large to fit in an int: " + count, e);
        }
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Override
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 添加评论
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
//        int rows = commentMapper.insertComment(comment);
        int rows = commentMapper.insert(comment);

        // 更新帖子评论数量
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
//            int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
            long count = count(new QueryWrapper<Comment>().eq("entity_type", comment.getEntityType()).eq("entity_id", comment.getEntityId()).eq("status",0));
            DiscussPost post = discussPostService.getById(comment.getEntityId());//根据帖子的id查
            post.setCommentCount(Math.toIntExact(count));
            discussPostService.updateById(post);
        }

        return rows;
    }
}
